//
//  NoteViewController.swift
//  FastDevelop
//
//  Created by IOS1 on 2025/6/27.
//

import UIKit

class NoteViewController: BaseViewController {

    private var dataSource = [Entry]()
    private var chooseDate = "" {
        didSet{
            loadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    private func setupUI(){
        
        let calendarView = CalendarView()
        calendarView.updateDataBlock = { choosedate in
            self.chooseDate = choosedate
        }
        calendarView.translatesAutoresizingMaskIntoConstraints = false
        
        view.addSubviews([
            calendarView,
            tableView,
            addButton
        ])

        calendarView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalToSuperview().offset(CGFloat.statusHeight)
            make.height.equalTo(300)
        }
        
        addButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.bottom.equalToSuperview().offset(-(CGFloat.tabBar_height + 33))
            make.width.height.equalTo(68)
        }
        
        tableView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(calendarView.snp.bottom)
            make.bottom.equalToSuperview().offset(-CGFloat.tabBar_height)
        }
        
        let today = Date()
        let writeDate = today.formattedCalendarDate()
        chooseDate = writeDate
    }
    
    private func loadData(){
        
        let logs = loadDailyLogs(forKey: "dailyLogs")
        if let log = logs.first(where: { $0.date == chooseDate }) {
            self.dataSource = log.data
        }else{
            self.dataSource = [Entry]()
        }
        self.tableView.reloadData()
    }

    @objc private func clickAddButton(){
                
        let addVC = AddNoteViewController()
        addVC.writeDate =  chooseDate
        self.navigationController?.pushViewController(addVC, animated: true)
    }
    
    private lazy var addButton: UIButton = {
        let v = UIButton(type: .custom)
        v.setImage(UIImage(named: "home_add"), for: .normal)
        v.addTarget(self, action: #selector(clickAddButton), for: .touchUpInside)
        return v
    }()
    
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(NoteListCell.self, forCellReuseIdentifier: "NoteListCell")
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 200.0
        return tableView
    }()
}

extension NoteViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.cell(NoteListCell.self, indexPath: indexPath)
        cell.model = dataSource[indexPath.row]
        return cell
    }
}
